<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Content extends Admin_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('admin/'. __CLASS__ .'_model', 'model');
	}

	public function index() {
		$data = [];

		$this->load->model('admin/Category_model');
		$data['infocate'] = $this->sort_level($this->Category_model->get_lists(['id', 'parent_id', 'name']));

		$where = NULL;
		$like = [];
		$get = $this->input->get(NULL, TRUE);

		if (! empty($get['category_id'])) {
			$category_id = [intval($get['category_id'])];
			foreach ($data['infocate'] as $key => $value) {
				in_array($value['parent_id'], $category_id) AND $category_id[] = $value['id'];
			}
			$where .= $this->db->dbprefix .'content.category_id IN('. implode(',', $category_id) .') AND ';
		}
		if (! empty($get['status'])) {
			$where .= $this->db->dbprefix .'content.status = '. (intval($get['status']) - 1) .' AND ';
		}
		if (! empty($get['keywords'])) {
			$like[$this->db->dbprefix .'content.title'] = trim($get['keywords']);
		}

		if (NULL == $where) {
			$where = [];
		} else {
			$where = rtrim(trim($where), 'AND');
		}

		$limit = 10;
		$config = [
			'base_url' => str_replace(config_item('url_suffix'), '', site_url('admin/content/index')),
			'total_rows' => $this->model->total_rows($where, $like),
			'per_page' => $limit,
			'uri_segment' => 4,
		];
		$data['page'] = $this->page($config);

		$offset = (int) $this->uri->segment($config['uri_segment'], 0);

		$data['lists'] = $this->model->get_lists($limit, $offset, ['content.*'], $where, TRUE, $like);

		$data['nav'] = $this->get_nav();

		$this->load->view($this->view, $data);
	}

	//显示添加模板
	public function add() {
		$data = [];

		//添加
		if ('POST' == $this->input->server('REQUEST_METHOD')) {
			$type = $this->input->get('type');
			if ('image' == $type) {
				$image = $this->upload(['upload_path' => './static/uploads/content/']);
				exit(json_encode($image));
			}

			$lang = config_item('lang');
			$insert_id = $this->model->insert();
			if (empty($insert_id)) {
				$this->error([
					'url' => $this->input->server('HTTP_REFERER'),
					'message' => $this->model->error,
				]);
			}
			$this->success([
				'url' => current_url(),
				'message' => $lang['action_success'],
			]);
		}

		$this->load->model('admin/Category_model');
		$data['category'] = $this->sort_level($this->Category_model->get_lists(['id', 'parent_id', 'name']));

		$data['nav'] = $this->get_nav();

		$data['kindeditor'] = TRUE;
		
		$this->load->view($this->view, $data);
	}

	//显示编辑模板
	public function edit($id = 0) {
		$data = [];
		$lang = config_item('lang');
		
		$id = (int) $this->uri->segment(4, 0);
		if (empty($id)) {
			$this->error([
				'url' => site_url('admin/content/index'),
				'message' => $lang['method_illegal'],
			]);
		}
		
		$where = ['id' => $id];
		//编辑
		if ('POST' == $this->input->server('REQUEST_METHOD')) {
			$type = $this->input->get('type');
			if ('image' == $type) {
				$image = $this->upload(['upload_path' => './static/uploads/content/']);
				exit(json_encode($image));
			}

			$affected_rows = $this->model->update($where);
			if (empty($affected_rows)) {
				$this->error([
					'url' => $this->input->server('HTTP_REFERER'),
					'message' => $lang['action_error'],
				]);
			}
			$this->success([
				'url' => site_url('admin/content/index'),
				'message' => $lang['action_success'],
			]);
		}

		$data['row'] = $this->model->get_one($where);
		if (empty($data['row'])) {
			$this->error([
				'url' => site_url('admin/content/index'),
				'message' => $lang['data_illegal'],
			]);
		}

		$this->load->model('admin/Category_model');
		$data['category'] = $this->sort_level($this->Category_model->get_lists(['id', 'parent_id', 'name']));

		$data['nav'] = $this->get_nav();

		$data['kindeditor'] = TRUE;
		
		$this->load->view($this->view, $data);
	}

	//删除
	public function del() {
		$lang = config_item('lang');
		$id = (int) $this->uri->segment(4, 0);
		if (empty($id)) {
			$this->error([
				'url' => $this->input->server('HTTP_REFERER'),
				'message' => $lang['method_illegal'],
			]);
		}

		$where = ['id' => $id];
		$affected_rows = $this->model->delete($where);
		if (empty($affected_rows)) {
			$this->error([
				'url' => $this->input->server('HTTP_REFERER'),
				'message' => $lang['action_error'],
			]);
		}
		$p = $this->uri->segment(5, 0) ? '/'. $this->uri->segment(5, 0) : '0';
		redirect('admin/content/index/'. $p);
	}
}
